<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="geofenceSearchForm.name" @keydown.enter="loadGeo"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;"  >
        <el-select placeholder="请选择" v-model="geofenceSearchForm.status" @keydown.enter="loadGeo" >
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeo">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status" @click=""></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="deleteGeofence(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="addVehicle(0)">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeGeofence(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog :before-close="handleClose" title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible">
    <el-table :data="unBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="vehicleStatusFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bindVehicle(scope.row.id )">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios, {create} from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
import router from "@/router";
var geoId ;
//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);

const geofenceSearchForm = ref({name:'',status:''});

const geoTableData =ref([]);

const bindVehicleArr = ref([]);

const loadGeo = ()=>{
  let data = qs.stringify(geofenceSearchForm.value);
  console.log(data);
  axios.get(BASE_URL+"/v1/geofence/select?"+data)
      .then((response)=>{
        if (response.data.code ===2000){
          console.log(response.data.data);
          geoTableData.value=response.data.data;
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}


const resetSearch=()=>{
  geofenceSearchForm.value={name:'',status:''};
  loadGeo();
}

const loadVehicle = (geofenceId)=>{
  geoId=geofenceId;
  bindVehicleDialogVisible.value=true;
  axios.get(BASE_URL+"/v1/vehicle/select?geofenceId="+geofenceId)
      .then((response)=>{
        if (response.data.code == 2000){
          bindVehicleArr.value=response.data.data;
        }else {
          ElMessage.error(response.data.msg);
        }
      })

}
const unBindVehicleArr = ref([]);

const addVehicle=(geofenceBindStatus)=>{
  AddBindVehicleDialogVisible.value=true;
  axios.get(BASE_URL+"/v1/vehicle/select?geofenceBindStatus="+geofenceBindStatus)
      .then((response)=>{
        if (response.data.code == 2000){
          unBindVehicleArr.value=response.data.data;
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
//移除围栏下的车辆
const removeGeofence =(id)=>{
  const vehicle = ref({id:id , geofenceBindStatus:0 });
  let data = qs.stringify(vehicle.value);
  axios.post(BASE_URL+"/v1/vehicle/save",data)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success("移除成功");
          loadVehicle(geoId);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

const bindVehicle =(id)=>{
  const vehicle = ref({id:id , geofenceBindStatus:1 ,geofenceId:geoId});
  let data = qs.stringify(vehicle.value);
  axios.post(BASE_URL+"/v1/vehicle/save",data)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success("绑定成功");
          addVehicle(0);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

const deleteGeofence = (geofenceId)=>{
  if (confirm('确定要删除围栏信息吗?(绑定在该围栏下的所有车辆也会被释放)')){
    axios.post(BASE_URL+"/v1/geofence/delete/"+geofenceId)
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success('删除成功');
            const vehicle = ref({geofenceId:geofenceId , geofenceBindStatus:0 });
            let data = qs.stringify(vehicle.value);
            axios.post(BASE_URL+"/v1/vehicle/save",data)
                .then((response)=>{
                  if (response.data.code == 2000){
                    //刷新一次页面
                    loadGeo();
                  }else {
                    ElMessage.error(response.data.msg);
                  }
                })
          }else{
            ElMessage.error(response.data.msg)
          }
        });
  }
}

const  vehicleTypeFormatter =(r,c,v,i)=>{
  if (v==="10"){
    v="轿车";
  }else if (v==="20"){
    v="货车";
  }else if (v==="30"){
    v="客车";
  }else if (v==="40"){
    v="挂车";
  }
  return v;
}
const  vehicleStatusFormatter =(r,c,v,i)=> {
  if (v === "1") {
    v = "空闲";
  } else if (v === "2") {
    v = "占用";
  } else if (v === "3") {
    v = "维修";
  }
  return v;
}


const  batteryTypeFormatter =(r,c,v,i)=>{
  if (v==="10"){
    v="铅酸蓄电池";
  }else if (v==="20"){
    v="溴氢电池";
  }else if (v==="30"){
    v="钠硫电池";
  }else if (v==="40"){
    v="二次锂电池";
  }else if (v==="50"){
    v="空气电池";
  }else if (v==="60"){
    v="三元锂电池";
  }else if (v==="70"){
    v="碱性锂电池";
  }
  return v;
}

const handleClose = ()=>{
  loadVehicle(geoId);
  AddBindVehicleDialogVisible.value=false
}


onMounted(()=>{
  loadGeo();
})

</script>

<style scoped>
</style>